package com.youxin.busfoundation.net;

import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.youxin.foundation.utils.JsonUtils;

import net.sf.json.JSONObject;

/**
 * 网络服务健康探测
 * 
 * @author yingde.cao
 *
 */
public class WebDetectController {

	public static String httpGet(String url, Object... uriVariables) {
		RestTemplate restTemplate = new RestTemplate();
		return restTemplate.getForObject(url, String.class, uriVariables);
	}

	public static ResponseEntity<String> httpGetEntity(String url, Object... uriVariables) {
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, uriVariables);
		return response;
	}

	/**
	 * http获取结果和status
	 * 
	 * @param url
	 * @return
	 */
	public static ResponseEntity<String> httpGetEntity(String url) {
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
		return response;
	}

	/**
	 * 获取链接状况,是否成功,当返回结果为200表示成功
	 * 
	 * @param url 链接地址
	 * @return boolean 是否成功
	 */
	public static boolean httpGetSuccess(String url) {
		RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
		ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
		if (response.getStatusCode().value() == HttpStatus.OK.value()) {
			return true;
		}
		return false;
	}

	private static ClientHttpRequestFactory getClientHttpRequestFactory() {
		int timeout = 10000;
		HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
		httpRequestFactory.setConnectionRequestTimeout(timeout);
		httpRequestFactory.setConnectTimeout(timeout);
		httpRequestFactory.setReadTimeout(timeout);
		return httpRequestFactory;
	}

	public static String httpGet(String url) {
		RestTemplate restTemplate = new RestTemplate();
		return restTemplate.getForObject(url, String.class);
	}

	/**
	 * post请求
	 * 
	 * @param url
	 *            url链接
	 * @param postParams
	 *            参数body参数,转换为JSON String
	 * @return String
	 */
	public static String httpPost(String url, Map<String, String> postParams) {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		JSONObject jsonObj = JsonUtils.mapToSfJson(postParams);
		HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
		String result = restTemplate.postForObject(url, formEntity, String.class);
		return result;
	}

}
